<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_structures = valueExists((int)$_GET['id'], "CODESTRUCTURE", "structure", $connexion);
			
			if($nb_structures == 1){ // la structure à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner une seule structure
	- La structure doit exister dans la BDD

*****************************************/

	// récupération des informations de la structure
	$query_recuperation_structure = "SELECT s.NOMSTRUCTURE AS nom, t.NOMTYPESTRUCTURE AS type, s.CODEJB AS code, s.ADRESSESTRUCTURE AS adresse, c.CODEPOSTAL AS cp, c.NOMCOMMUNE AS ville, a.NOMPAYS AS pays, s.EMAILSTRUCTURE AS email, s.TELEPHONESTRUCTURE AS telephone, s.FAXSTRUCTURE AS fax, s.SITEINTERNET AS site, s.DATEDERNIERCONTACT AS dernierContact, s.COMMENTAIRE AS commentaire, p.CODEPERSONNE AS idPersonne, p.NOMPERSONNE AS nomPersonne, p.PRENOMPERSONNE AS prenomPersonne ";
	$query_recuperation_structure .= "FROM structure s ";
	$query_recuperation_structure .= "INNER JOIN type_structure t ON s.CODETYPESTRUCTURE = t.CODETYPESTRUCTURE ";
	$query_recuperation_structure .= "LEFT JOIN commune c INNER JOIN pays a ON c.CODEPAYS = a.CODEPAYS ON s.CODECOMMUNE = c.CODECOMMUNE ";
	$query_recuperation_structure .= "LEFT JOIN personne p ON s.CODESTRUCTURE = p.CODESTRUCTURE ";
	$query_recuperation_structure .= "WHERE s.CODESTRUCTURE = ".$_GET['id']." ";
	$query_recuperation_structure .= "ORDER BY nomPersonne, prenomPersonne ";
	
	$result_recuperation_structure = mysql_query($query_recuperation_structure, $connexion) or logError("RECUPERATION STRUCTURE-".$query_recuperation_structure."-".mysql_error());
	
	$structure = array(); // on créé un tableau contenant toutes les informations de la structure
	while($tab_recuperation_structure = mysql_fetch_assoc($result_recuperation_structure)){
		
		$structure['nom'] = $tab_recuperation_structure['nom'];
		$structure['type'] = $tab_recuperation_structure['type'];
		$structure['code'] = $tab_recuperation_structure['code'];
		$structure['adresse'] = $tab_recuperation_structure['adresse'];
		$structure['cp'] = $tab_recuperation_structure['cp'];
		$structure['ville'] = $tab_recuperation_structure['ville'];
		$structure['pays'] = $tab_recuperation_structure['pays'];
		$structure['email'] = $tab_recuperation_structure['email'];
		$structure['telephone'] = $tab_recuperation_structure['telephone'];
		$structure['fax'] = $tab_recuperation_structure['fax'];
		$structure['site'] = $tab_recuperation_structure['site'];
		$structure['dernierContact'] = $tab_recuperation_structure['dernierContact'];
		$structure['commentaire'] = $tab_recuperation_structure['commentaire'];
		if(!is_null($tab_recuperation_structure['idPersonne'])){
			$structure['personnel'][$tab_recuperation_structure['idPersonne']]['id'] = $tab_recuperation_structure['idPersonne'];
			$structure['personnel'][$tab_recuperation_structure['idPersonne']]['nom'] = $tab_recuperation_structure['nomPersonne'];
			$structure['personnel'][$tab_recuperation_structure['idPersonne']]['prenom'] = $tab_recuperation_structure['prenomPersonne'];
		}
		
	}
	
	mysql_free_result($result_recuperation_structure);
	
?>
<h3>Détail d'une structure</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<td colspan="2">
			<b><?php echo htmlentities($structure['nom'], ENT_NOQUOTES, "UTF-8"); ?></b>
		</td>
	</tr>
	<tr>
		<th colspan="2">Informations générales</th>
	</tr>
	<tr>
		<td class="etiquette" style="width: 34%;">Type:</td>
		<td style="width: 66%;">
			<?php 
				echo htmlentities(mb_strtolower($structure['type']), ENT_NOQUOTES, "UTF-8");
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Code:</td>
		<td>
			<?php 
				if(!is_null($structure['code'])){
					echo htmlentities($structure['code'], ENT_NOQUOTES, "UTF-8")."<br />";
				}else{
					echo "<i>aucun code attribué</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Contact</th>
	</tr>
	<tr>
		<td class="etiquette">Adresse:</td>
		<td>
			<?php 
				if(!is_null($structure['adresse'])){
					echo htmlentities($structure['adresse'], ENT_NOQUOTES, "UTF-8")."<br />";
				}
				echo htmlentities(capitalise($structure['cp']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($structure['ville']), ENT_NOQUOTES, "UTF-8")." - ".htmlentities(mb_strtoupper($structure['pays']), ENT_NOQUOTES, "UTF-8") ;
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Téléphone:</td>
		<td>
			<?php
				if(!is_null($structure['telephone'])){
					echo htmlentities($structure['telephone'], ENT_NOQUOTES, "UTF-8");
				}else{
					echo "<i>aucun numéro indiqué</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Fax:</td>
		<td>
			<?php
				if(!is_null($structure['fax'])){
					echo htmlentities($structure['fax'], ENT_NOQUOTES, "UTF-8");
				}else{
					echo "<i>aucun numéro indiqué</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">E-mail:</td>
		<td>
			<?php
				if(!is_null($structure['email'])){
					echo "<a href=\"mailto:".$structure['email']."\">".htmlentities($structure['email'], ENT_NOQUOTES, "UTF-8")."</a>";
				}else{
					echo "<i>aucun e-mail indiqué</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Informations complémentaires</th>
	</tr>
	<tr>
		<td class="etiquette">Site:</td>
		<td>
			<?php 
				if(!is_null($structure['site'])){
					echo "<a href=\"".htmlentities($structure['site'], ENT_NOQUOTES, "UTF-8")."\" onclick=\"firstWindow().open(this.href, '', ''); return false;\">".htmlentities(truncate($structure['site'], 30), ENT_NOQUOTES, "UTF-8")."</a>";
				}else{
					echo "<i>aucun site indiqué</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Dernier contact:</td>
		<td>
			<?php 
				if(!is_null($structure['dernierContact'])){
					echo dateFr($structure['dernierContact']);
				}else{
					echo "<i>inconnu</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Personnel</th>
	</tr>
	<tr>
		<td colspan="2">
			<?php 
				if(isset($structure['personnel'])){
					echo "<ul>";
					foreach($structure['personnel'] as $personne){
						echo "<li><a href=\"popup.php?p=gestion/personne_affiche_popup&amp;id=".$personne['id']."\" onclick=\"popup(this.href, 350, 400); return false;\">".htmlentities(capitalise($personne['prenom']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(mb_strtoupper($personne['nom']), ENT_NOQUOTES, "UTF-8")."</a></li>";
					}
					unset($personne);
					echo "</ul>";
				}else{
					echo "<i>aucune personne indiquée</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Commentaire</th>
	</tr>
	<tr>
		<td class="texte" colspan="2"><?php echo nl2br(htmlentities($structure['commentaire'], ENT_NOQUOTES, "UTF-8")); ?></td>
	</tr>
</table>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("La structure n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("La structure n'est pas ou mal indiqué", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>